package com.twu.biblioteca;

/**
 * Created by olga on 7/06/14.
 */
public class LibraryItem
{
    private String title;
    private String creator;
    private String year;
    protected String itemType;
    private boolean isCheckedOut;
    private User userCheckedOut;

    public LibraryItem(String title, String creator, String year)
    {
        setTitle(title);
        setCreator(creator);
        setYear(year);
        itemType = "";
        isCheckedOut = false;
    }

    private void setTitle(String title)
    {
        this.title = (title == null) ? "" : title;
    }

    public String getTitle()
    {
        return title;
    }

    private void setCreator(String creator)
    {
        this.creator = (creator == null) ? "" : creator;
    }

    public String getCreator()
    {
        return creator;
    }

    private void setYear(String year)
    {
        this.year = (year == null) ? "" : year;
    }

    public String getYear()
    {
        return year;
    }

    public boolean isCheckedOut()
    {
        return isCheckedOut;
    }

    public LibraryItem setCheckedOutStatus(boolean isBookCheckedOut)
    {
        if( this.isCheckedOut == isBookCheckedOut )
            return null;
        else
        {
            changeCheckOutStatus(isBookCheckedOut);
            return this;
        }
    }

    public String getItemType() { return itemType; }

    public void setItemType(String itemType) {
        this.itemType = itemType;
    }

    public int getCreatorStringLength()
    {
        return this.creator.length();
    }

    public int getTitleStringLength()
    {
        return this.title.length();
    }

    public void setUserCheckedOut(User userCheckedOut)
    {
        this.userCheckedOut = userCheckedOut;
    }

    public User getUserCheckedOut() {
    return userCheckedOut;
}

    @Override
    public boolean equals(Object obj)
    {
        if( !(obj instanceof LibraryItem) )
            return false;
        else
            return ((LibraryItem)obj).getTitle().toUpperCase().equals( this.getTitle().toUpperCase() ) &&
                    ((LibraryItem)obj).getCreator().toUpperCase().equals( this.getCreator().toUpperCase() ) &&
                    ((LibraryItem)obj).getYear().toUpperCase().equals(this.getYear().toUpperCase()) &&
                    (((LibraryItem)obj).getItemType().equals(this.getItemType()));
    }


    private void changeCheckOutStatus( boolean newStatus)
    {
        if( !newStatus )
            this.userCheckedOut = null;
        this.isCheckedOut = newStatus;
    }
}
